package top.linyuxb.lycloud.rpc.starter.common;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import top.linyuxb.lycloud.rpc.starter.annotation.LyRpcServer;
import top.linyuxb.lycloud.rpc.starter.aop.RpcSession;
import top.linyuxb.lycloud.rpc.starter.properties.LyRpcClientProperties;
import top.linyuxb.lycloud.rpc.starter.properties.LyRpcServerProperties;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * Created by Intellij IDEA.
 *
 * @Author: linyuxb
 * @Date: 2024/6/4 19:07
 * @Desc: bean解析工具
 */
public class LyRpcBeanUtil {
    /**
     * 获取客户端代理bean
     *
     * @param lyRpcClientProperties
     * @return
     */
    public static Map<String, Object> getClientBeanMap(LyRpcClientProperties lyRpcClientProperties) {
        Map<String, Object> servers = MapUtil.newHashMap();
        Set<Class<?>> classes = getLyRpcClasses(lyRpcClientProperties.getClasses());
        for (Class<?> aClass : classes) {
            LyRpcServer lyRpcServer = aClass.getAnnotation(LyRpcServer.class);
            if (Objects.nonNull(lyRpcServer)) {
                Object server = RpcSession.getServer(aClass, lyRpcServer, lyRpcClientProperties);
                servers.put(getClassName(lyRpcServer.name(), aClass), server);
            }
        }
        return servers;
    }

    /**
     * 获取rpc接口map
     *
     * @param lyRpcServerProperties
     * @return
     */
    public static Map<String, Class<?>> getServerClassMap(LyRpcServerProperties lyRpcServerProperties) {
        Map<String, Class<?>> classMap = MapUtil.newHashMap();
        Set<Class<?>> classes = getLyRpcClasses(lyRpcServerProperties.getClasses());
        for (Class<?> aClass : classes) {
            LyRpcServer lyRpcServer = aClass.getAnnotation(LyRpcServer.class);
            classMap.put(getClassName(lyRpcServer.name(), aClass), aClass);
        }
        return classMap;
    }

    /**
     * 获取类名称
     *
     * @param name
     * @param aClass
     * @return
     */
    public static String getClassName(String name, Class<?> aClass) {
        return StrUtil.isNotBlank(name) ? name : StrUtil.lowerFirst(aClass.getSimpleName());
    }

    /**
     * 获取rpc服务接口
     *
     * @param classes
     * @return
     */
    public static Set<Class<?>> getLyRpcClasses(String classes) {
        Set<Class<?>> classSet = CollUtil.newHashSet();
        if (StrUtil.isNotBlank(classes)) {
            String[] split = classes.split(",");
            for (String path : split) {
                Set<Class<?>> classes1 = ClassUtil.scanPackageByAnnotation(path, LyRpcServer.class);
                if (CollUtil.isNotEmpty(classes1)) {
                    classSet.addAll(classes1);
                }
            }
        } else {
            Set<Class<?>> classes1 = ClassUtil.scanPackageByAnnotation(null, LyRpcServer.class);
            if (CollUtil.isNotEmpty(classes1)) {
                classSet.addAll(classes1);
            }
        }
        return classSet;
    }

}
